﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.IO;

namespace TTCaller
{
    public partial class JobCancelPanel : UserControl
    {


        public JobCancelPanel()
        {
            InitializeComponent();
        }


        string m_strLastCMD = "";
        MsgType m_mt;
        public void initial(string arg_strCMD, MsgType arg_Type)
        {
            m_strLastCMD = arg_strCMD;
            m_mt = arg_Type;
            LogManager.getInstance().Log("initial DCP:" + arg_Type.ToString() + ":" + arg_strCMD);
            this.label_NoExecute.Visible = false;

            switch (m_mt)
            {

                case MsgType.Dispatch:

                    this.textBox_Msg.Text = Form_Main.Main.getBodyText(m_strLastCMD);
                    TrimMessageEnd();

                    this.button_F1.Text = "上一頁";
                    this.button_F1.Visible = true;

                    if (m_strLastCMD[0] == 'J' || m_strLastCMD[0] == 'P')
                    {
                        this.button_F4.Text = "取消用車";
                        this.button_F4.Visible = true;
                        this.label_NoExecute.Visible = false;
                    }
                    else
                    {
                        this.button_F4.Visible = false;

                    }

                    this.button_F2.Text = "主畫面";
                    this.button_F2.Visible = true;

                    break;

                case MsgType.Msg:

                    break;

            }

        }

        /// <summary>
        /// 由於派遣地址尾端有經緯度, 因此要清除掉
        /// </summary>
        private void TrimMessageEnd()
        {
            int nIndex = this.textBox_Msg.Text.IndexOf('\t');
            if (nIndex != -1)
            {
                textBox_Msg.Text = textBox_Msg.Text.Split('\t')[0];
            }

        }

        private void button_F2_Click(object sender, EventArgs e)
        {
            switch (m_mt)
            {
                case MsgType.Dispatch:
                    Form_Main.Main.ChangeToMainScreen();
                    break;

            }

        }


        private void button_F1_Click(object sender, EventArgs e)
        {
            switch (m_mt)
            {
                case MsgType.Dispatch:
                    Form_Main.Main.ChangeToMsgList(m_mt);
                    break;

            }
        }


        private void button_F4_Click(object sender, EventArgs e)
        {
            switch (m_mt)
            {
                case MsgType.Dispatch:
                    //取消用車..PA01100316154626000001010ND
                    string strTemp = "OJ" + m_strLastCMD.Substring(3, 15) + "N" + m_strLastCMD.Substring(18, 4);

                    string strResult = CmdTalker.getInstance().SendServerAndWaitResponse(strTemp, 5, "MOK", true);

                    if (strResult == "MOK")
                    {
                        //wait result
                        Form_Main.Main.updateDispatchRec(m_strLastCMD.Substring(18, 4), 'C');
                        Form_Main.Main.ChangeToMainScreen();
                    }
                    else
                    {
                        //fail , re send, 
                        this.label_NoExecute.Text = "抱歉, 取消失敗, 請來電通知. 謝謝.";
                        this.label_NoExecute.Visible = true;
                        LogManager.getInstance().Log("Send Fail:" + strTemp, false);
                    }


                    break;

            }
        }


    }
}
